<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // bind 1. 偏函数的特性  2. 原型的问题

        // 1. 原生
        // function fn(a, b) {
        //     return a + b
        // }

        // console.log(fn.bind({}, 1, 2)())
        // console.log(fn.bind({}, 1)(2))
        // console.log(fn.bind({})(1, 2))
        // // console.log(fn.bind({})(1)(2)) // 注意: 这种写法不行

        // function People(name) {
        //     this.name = name
        // }

        // People.prototype.say = function () {
        //     return 'i can say'
        // }
        // var p1 = new People('小红')
        // console.log('p1.say()', p1.say())
        // console.log('p1.__proto__', p1.__proto__)
        // console.log('People.prototype', People.prototype)


        // var People2 = People.bind({})
        // var p2 = new People2('小明')
        // console.log('p2.say()', p2.say())
        // console.log('p2.__proto__', p2.__proto__)
        // console.log('People2.prototype', People2.prototype)

        // console.log(p1.__proto__ === p2.__proto__) // true
        // console.log(People.prototype === People2.prototype) // false

        // 2.重写

        Function.prototype._bind = function (context) {
            var that = this
            var args = Array.prototype.slice.call(arguments, 1)
            var result = function () {
                var innerArgs = Array.prototype.slice.call(arguments)
                return that.apply(context, args.concat(innerArgs))
            }
            // es6写法
            // result.prototype = Object.create(this.prototype) 

            // es5写法
            var Temp = function () { }
            Temp.prototype = this.prototype
            result.prototype = new Temp()
            return result
        }

        function fn(a, b) {
            return a + b
        }

        console.log(fn._bind({}, 1, 2)())
        console.log(fn._bind({}, 1)(2))

        function People(name) {
            this.name = name
        }

        People.prototype.say = function () {
            return 'i can say'
        }
        var p1 = new People('小红')
        console.log('p1.say()', p1.say())
        console.log('p1.__proto__', p1.__proto__)
        console.log('People.prototype', People.prototype)

        var People2 = People._bind({})
        var p2 = new People2('小明')
        console.log('p2.say()', p2.say())
        console.log('p2.__proto__', p2.__proto__)
        console.log('People2.prototype', People2.prototype)

        console.log(p1.__proto__ === p2.__proto__)
        console.log(People.prototype === People2.prototype)


    </script>
</body>

</html>